<?php
declare (strict_types=1);

namespace app\api\controller;

use think\exception\ValidateException;
use think\facade\View;
use think\response\Json;
use app\common\model\Order as OrderModel;

class Order extends Base
{
    public function index()
    {
        $temporary = OrderModel::getTemporary($this->user->id);

        if (empty($temporary) || empty($temporary['products'])) {
            return json(['code' => 1, 'msg' => '加载失败，未查询到待提交的订单']);
        }
        unset($temporary['address']);
        return json(['code' => 1, 'msg' => 'success', 'result' => $temporary]);
    }

    public function submit()
    {
        $address_id = $this->request->post('address_id');
        $result = OrderModel::submit($address_id, $this->user);
        $result['code'] = $result['code'] >= 1 ? 1 : -1;
        return json($result);
    }

    public function payment()
    {
        $info = OrderModel::getNowOrder($this->user->id);
        if ($info['code'] != 1) {
            return json(['code' => -1, 'msg' => '加载失败，未查询到待支付的订单']);
        }
        $result['total'] = $info['result']['total_amount'];

        $result['method'] = [
            [
                'title' => '微信支付',
                'pay_way' => 'wechat',
                'icon' => handleImg('/static/index/img/wechat.png')
            ],
            [
                'title' => '电子币',
                'pay_way' => 'currency',
                'icon' => handleImg('/static/index/img/currency.png')
            ],
            [
                'title' => '奖金',
                'pay_way' => 'bonus',
                'icon' => handleImg('/static/index/img/bonus.png')
            ],
        ];
        $result['is_pay_password'] = 1;
        return json(['code' => 1, 'msg' => 'message', 'result' => $result]);

    }

    public function confirmPayment(): Json
    {
        $param = $this->request->post();
        try {
            $this->validate($param, 'ConfirmPayment');
        } catch (ValidateException $e) {
            return json(['code' => -1, 'msg' => $e->getError()]);
        }
        if (md5($param['pay_password']) != $this->user->pay_password) {
            return json(['code' => -1, 'msg' => '支付密码错误']);
        }
        return json(OrderModel::confirmPayment($param, $this->user));
    }

    public function my_order()
    {
        return View::fetch();
    }

    public function my_order_details()
    {
        $id = $this->request->param('id');
        $info = OrderModel::where(['uid' => $this->user->id, 'id' => $id])->find();
        if (empty($info)) {
            return json(['code' => -1, 'msg' => '未查询到相关订单']);
        }
        if ($info->getData('status') < 3) {
            $info->addressData = [
                'receiver' => $info->address->nickname,
                'receiver_tel' => $info->address->tel,
                'province' => $info->address->p->title,
                'city' => $info->address->c->title,
                'district' => $info->address->d->title,
                'town' => $info->address->t->title,
                'details' => $info->address->details
            ];
        } else {
            $info->addressData = [
                'receiver' => $info->receiver,
                'receiver_tel' => $info->receiver_tel,
                'province' => $info->p->title,
                'city' => $info->c->title,
                'district' => $info->d->title,
                'town' => $info->t->title,
                'details' => $info->address_details
            ];
            $info->expressData = [
                'express_id' => $info->express_id,
                'express_code' => $info->express_code,
                'express_title' => $info->express->title,
            ];
        }
        $info->productsData = $info->products;
        $date = ['pay_time', 'receipt_time', 'comment_time', 'finish_time', 'refund_time', 'cancel_time',
            'remind_time', 'post_time'];
        foreach ($date as $v) {
            $info->$v = $info->$v ? date('Y-m-d H:i:s', intval($info->$v)) : '';
        }
        $unset = ['receiver', 'receiver_tel', 'province', 'city', 'district', 'town', 'address_details',
            'address', 'uid', 'address_id', 'is_del', 'order_type', 'express', 'express_id', 'express_code', 'update_time',
            'p', 'c', 'd', 't','products'];
        foreach ($unset as $v) {
            unset($info->$v);
        }

        return json(['code' => 1, 'msg' => 'Message', 'result' => $info]);

    }

    public function getList(): Json
    {
        $param = $this->request->param();
        if (isset($param['type']) && $param['type']) {
            $map = ['uid' => $this->user->id, 'status' => $param['type'], 'is_del' => 0];
        } else {
            $map = ['uid' => $this->user->id, 'is_del' => 0];
        }
        $list = OrderModel::getPageList($map, 'id,sn,pay_way,status,create_time,total_amount,is_remind', $param['page'], 20, false, ['products']);
        return json(['code' => 1, 'msg' => 'success', 'result' => $list]);

    }

    public function edit(): Json
    {
        $param = $this->request->post();
        $result = OrderModel::edit($param, $this->user);
        $result['code'] = $result['code'] >= 1 ? 1 : -1;
        return json($result);
    }

}


